package com.lu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lu.constants.HttpStatusEnum;
import com.lu.constants.LinkConstants;
import com.lu.dto.AjaxPutPage;
import com.lu.dto.AjaxResultPage;
import com.lu.dto.Result;
import com.lu.pojo.BlogLink;
import com.lu.service.BlogLinkService;
import com.lu.utils.DateUtils;
import com.lu.utils.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 友情链接表 前端控制器
 * </p>
 *
 * @author xiaolu
 * @since 2020-05-30
 */
@Controller
@RequestMapping("/admin")
public class LinkController {

    @Autowired
    private BlogLinkService blogLinkService;

    // 跳转到链接列表页面
    @GetMapping("/v1/linkType")
    public String linkType(){

        return "admin/link-list";
    }

    // 搜索框信息分类
    @GetMapping("/v1/linkType/list")
    @ResponseBody
    public Result<List<BlogLink>> list(){

       List<BlogLink> links = new ArrayList<>();
       links.add(new BlogLink().setLinkType(LinkConstants.LINK_TYPE_FRIENDSHIP.getLinkTypeId())
               .setLinkName(LinkConstants.LINK_TYPE_FRIENDSHIP.getLinkTypeName()));
       links.add(new BlogLink().setLinkType(LinkConstants.LINK_TYPE_RECOMMEND.getLinkTypeId())
               .setLinkName(LinkConstants.LINK_TYPE_RECOMMEND.getLinkTypeName()));
       links.add(new BlogLink().setLinkType(LinkConstants.LINK_TYPE_PRIVATE.getLinkTypeId())
               .setLinkName(LinkConstants.LINK_TYPE_PRIVATE.getLinkTypeName()));
       return ResultGenerator.getResultByHttp(HttpStatusEnum.OKK,links);
    }

    // 显示数据库信息
    @GetMapping("/v1/link/paging")
    @ResponseBody
    public AjaxResultPage<BlogLink> paging(AjaxPutPage<BlogLink> ajaxPutPage, BlogLink condition){

        QueryWrapper<BlogLink> queryWrapper = new QueryWrapper<>(condition);
        queryWrapper.lambda().orderByAsc(BlogLink::getLinkRank);
        Page<BlogLink> page = ajaxPutPage.putPageToPage();
        // 翻页查询
        blogLinkService.page(page,queryWrapper);
        AjaxResultPage<BlogLink> result = new AjaxResultPage<>();
        result.setData(page.getRecords());
        result.setCount(page.getTotal());
        return result;
    }

    // 删除信息(可恢复)
    @PostMapping("/v1/link/isDel")
    @ResponseBody
    public Result<String> isDel(BlogLink blogLink){

        boolean flag = blogLinkService.updateById(blogLink);
        if (flag){
            return ResultGenerator.getResultByHttp(HttpStatusEnum.OKK);
        }else {
            return ResultGenerator.getResultByHttp(HttpStatusEnum.UNAUTHORIZED);
        }
    }

    // 清除信息
    @PostMapping("/v1/link/clear")
    @ResponseBody
    public Result<String> clear(Integer linkId){

        boolean flag = blogLinkService.removeById(linkId);
        if (flag){
            return ResultGenerator.getResultByHttp(HttpStatusEnum.OKK);
        }else {
            return ResultGenerator.getResultByHttp(HttpStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    // 跳转到添加链接页面
    @GetMapping("/v1/link/edit")
    public String edit(Model model, Integer linkId){

        if (linkId != null){
            BlogLink blogLink = blogLinkService.getById(linkId);
            model.addAttribute("blogLink", blogLink);
        }
        return "admin/link-edit";
    }

    // 提交新添加的链接
    @PostMapping("/v1/link/edit")
    @ResponseBody
    public Result<String> edit(BlogLink blogLink){

        blogLink.setCreateTime(DateUtils.getLocalCurrentDate());
        boolean flag;
        if (blogLink.getLinkId() != null){
            flag = blogLinkService.updateById(blogLink);
        }else {
            flag = blogLinkService.save(blogLink);
        }
        if (flag){
            return ResultGenerator.getResultByHttp(HttpStatusEnum.OKK);
        }else {
            return ResultGenerator.getResultByHttp(HttpStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

}

